/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utility;

import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.*;

/**
 *
 * @author Allan
 */
public class DateChooser
{
    private int month;
    private int year;
    private String day;
    private JLabel lblShowMonth;
    private JButton[] btnDates;
    private JDialog dialog;
    
    public DateChooser(JFrame parent)
    {
        // Her sættes variabler der på en eller anden måde vedrører tiden
        month = Calendar.getInstance().get(Calendar.MONTH);
        year = Calendar.getInstance().get(Calendar.YEAR);
        day = "";
        String[] header = { "Søn", "Man", "Tirs", "Ons", "Tors", "Fre", "Lør" };
        
        // Her laves labels
        lblShowMonth = new JLabel("", JLabel.CENTER);

        // Her laves knapper
        JButton btnLastMonth;
        JButton btnNextMonth;
        btnDates = new JButton[49];
        btnLastMonth = new JButton("<< Tilbage");
        btnNextMonth = new JButton("Videre >>");
        
        // Her laves dialogboksen
        dialog = new JDialog();
        dialog.setModal(true);
        
        // Her laves panelerne
        JPanel panel1;
        JPanel panel2;
        panel1 = new JPanel(new GridLayout(7, 7));
        panel1.setPreferredSize(new Dimension(420, 180));
        panel2 = new JPanel(new GridLayout(1, 3));
        
        // Her tilføjes knapper og label til panel2
        panel2.add(btnLastMonth);
        panel2.add(lblShowMonth);
        panel2.add(btnNextMonth);
        
        // Her tilføjes panelerne til dialog
        dialog.add(panel1, BorderLayout.CENTER);
        dialog.add(panel2, BorderLayout.SOUTH);
        dialog.pack();
        dialog.setLocationRelativeTo(parent);
        
        // .....
        for(int x = 0; x < btnDates.length; x++)
        {
            final int selection = x;
            btnDates[x] = new JButton();
            btnDates[x].setFocusPainted(false);
            btnDates[x].setBackground(Color.white);
            
            if(x > 6)
            {
                btnDates[x].addActionListener(new ActionListener()
                {
                    @Override
                    public void actionPerformed(ActionEvent ae)
                    {
                        day = btnDates[selection].getActionCommand();
                        dialog.dispose();
                    }
                });
            }
            
            if(x < 7)
            {
                btnDates[x].setText(header[x]);
                btnDates[x].setForeground(Color.red);
            }
            
            panel1.add(btnDates[x]);
        }
        
        // Listeners begynder her
        btnLastMonth.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                month--;
                displayDate();
            }
        });
        
        btnNextMonth.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                month++;
                displayDate();
            }
        });
        
        // Her kaldes metoden displayDate()
        displayDate();
        
        // Her sættes dialog synlig
        dialog.setVisible(true);
    }

    public void displayDate()
    {
        for(int x = 7; x < btnDates.length; x++)
        {
            btnDates[x].setText("");
        }
        
        // Sætter datoformatet til at være Måned år (Maj 2013)
        SimpleDateFormat sdf = new SimpleDateFormat("MMMM yyyy");
        
        Calendar cal = Calendar.getInstance();
        cal.set(year, month, 1);
        
        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
        int daysInMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        
        for(int x = 6 + dayOfWeek, day = 1; day <= daysInMonth; x++, day++)
        {
            btnDates[x].setText("" + day);
        }
        
        lblShowMonth.setText(sdf.format(cal.getTime()));
        dialog.setTitle("Date Chooser");
    }

    public String setPickedDate()
    {
        if(day.equals(""))
        {
            return day;
        }
        
        // Sætter datoformatet til at være dag-måned-år (05-08-2013)
        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
        
        Calendar cal = Calendar.getInstance();
        cal.set(year, month, Integer.parseInt(day));
        
        return sdf.format(cal.getTime());
    }
}
